package cn.hj.trigger.job;

import cn.hj.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import cn.hj.domain.strategy.service.IRaffleStock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 为了防止数据压力过大，将扣减库存操作放到redis 消息队列更新库存缓存，异步队列更新数据库，数据库最终一致
 */
@Component
@Slf4j
public class UpdateAwardStockJob {

    @Resource
    private IRaffleStock raffleStock;


    @Scheduled(cron = "0/5 * * * * ?")
    public void updateAwardStock() {
        try {
            log.info("更新库存开始");
            StrategyAwardStockKeyVO queueValue = raffleStock.getQueueValue();
            if (queueValue == null) {
                return;
            }
            raffleStock.updateStock(queueValue.getStrategyId(), queueValue.getAwardId());
            log.info("更新库存结束");
        }catch (Exception e){
            log.error("定时任务，更新奖品库存异失败",e);
        }
    }
}
